package com.example.bes014.web.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

@RestController
public class TestController {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private MessageSource messageSource;

    @GetMapping(value = "/")
    public String hello() {
        return "welcome to use bes application server ~~~";
    }

    @GetMapping(value = "/testconnectionTimeout")
    public void testConnectionTimeout() throws InterruptedException, IOException {
        Socket socket = new Socket("127.0.0.1", 10017);
        System.out.println(LocalDateTime.now().format(formatter));
        try {
            InputStream is = socket.getInputStream();
            is.read();
        } catch (Exception e) {
            System.out.println(LocalDateTime.now().format(formatter));
        }
    }

    @GetMapping(value = "/greeting")
    public String greeting(Locale locale) {
        String greetingMessage = messageSource.getMessage("greeting", null, locale);

        return greetingMessage;
    }
}
